﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SGD;

namespace DirectXTest
{
    public partial class ImageLoadPrompt : Form
    {
        int textureID;
        string fileName;

        ManagedTextureManager texManager;
        ManagedDirect3D d3d;


        public ImageLoadPrompt()
        {
            textureID = -1;

            d3d = ManagedDirect3D.Instance;
            texManager = ManagedTextureManager.Instance;

            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|jpeg|*.jpg|png|*.png|bmp|*.bmp";
            dlg.FilterIndex = 1;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                fileName = dlg.FileName;
                textureID = texManager.LoadTexture(fileName, 0);
                imageWidthNumUpDown.Value = texManager.GetTextureWidth(textureID);
                imageHeightNumUpDown.Value = texManager.GetTextureHeight(textureID);
                pathTextBox.Text = fileName;

                float scaleX = ((float)canvasPanel.Width) / ((float)texManager.GetTextureWidth(textureID));
                float scaleY = ((float)canvasPanel.Height) / ((float)texManager.GetTextureHeight(textureID));

                if (scaleX < scaleY)
                    scaleY = scaleX;
                else
                    scaleX = scaleY;

                d3d.Clear(255, 255, 255);
                d3d.DeviceBegin();
                d3d.SpriteBegin();
                texManager.Draw(textureID, 0, 0, scaleX, scaleY, Rectangle.Empty, 0, 0, 0.0f, 0);
                d3d.SpriteEnd();
                d3d.DeviceEnd();

                d3d.Present();               
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
            }
        }

        public int TextureID
        {
            get
            {
                return textureID;
            }
            set
            {
                textureID = value;
            }
        }

        private void importButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
